# import os
# m="a"
# for i in range(99999):
#     file=open('E:'+ m+str(i)+".txt",'w')
#     file.write('guo.text')

# os.system(r"fsutil file createnew E:/8G.txt 8123456789")
# print(os.path)


# import time
# def creatfilesize(n):
#     local_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
#     print(local_time)
#     nn=int(n)
#     print(type(nn))
#     file_name = "E:\\" + str(local_time) + ".txt"
#     bigFile = open(file_name, 'w')
#     bigFile.seek(1024 * 1024 * 1024 * n)
#     bigFile.write('test')
#     # bigFile.write("test")
#     bigFile.close()
#     print("ALL down !")
# if __name__ == '__main__':
#     n = input("input:")
#     creatfilesize(n)

#写操作
from common.read_log import Mylog as Log
import os,time
log=Log()
path = r"D:\testFile"
delend = ".txt"


def counts(n,m):
    if float(n) > 1:
        log.error("请输入1G的文件大小")
        exit()
    if int(m)>=1000:
        log.error(("此次输入的数量%f大于U盘存储量,请重新输入")%int(m))
        print("你输入的次数超出U盘存储量，请重新输入！")
        exit()
    for i in range(int(m)):
        creatfilesize(n)
        if i==5:
            del_files(path,delend)

def creatfilesize(n):
    start = time.time()
    log.info("开始测试写入"+n+"GB"+"的文件")
    local_time = time.strftime("%Y%m%d%H%M%S",time.localtime())
    file_path="D:\\testFile"
    if not os.path.exists(file_path):
        os.makedirs(file_path)
    file_name = "D:\\testFile\\"+str(local_time)+".txt"
    bigFile= open(file_name, 'w')
    bigFile.seek(1024*1024*1024*float(n))
    bigFile.write('test')
    bigFile.close()
    end = time.time()
    print("循环运行时间:%.2f秒" % (end - start))
    log.info("写入"+n+"GB"+"的文件成功"+"使用时间:%.2f秒" % (end - start))
    print ("ALL down !")

#删除
def del_files(path, delend):
    log.info("开始清除U盘数据")
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith(delend):
                os.remove(os.path.join(root, name))
                print("delete file :", os.path.join(root, name))
    log.info("U盘数据清除成功！")


# if __name__ == '__main__':
#     path = r"E:\testFile"   # 路径E
#     delend = ".txt"     # 要删除的文件后缀
#     del_files(path, delend)

if __name__ == '__main__':
    n = input("输入你要生成的文件E大小（单位为G）:")
    m = input("输入你要执行的次数:")
    counts(n,m)